package com.tongbo.mobilesafe.receiver;

import com.tongbo.mobilesafe.R;
import com.tongbo.mobilesafe.db.dao.BlackNumberDao;
import com.tongbo.mobilesafe.engine.GPSInfoProvider;
import com.tongbo.mobilesafe.ui.QueryAddressActivity;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSReceiver extends BroadcastReceiver{
	private static final String TAG = "SMSReceiver";
	private BlackNumberDao dao;
	@Override
	public void onReceive(Context context, Intent intent) {
		dao = new BlackNumberDao(context);
		//获取短信内容如果是 #*location*#12345
		//获取gps信息
		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
		for(Object pdu:pdus){
			SmsMessage message = SmsMessage.createFromPdu((byte[])pdu);
			String content = message.getMessageBody();
			Log.i(TAG, "短信内容:"+content);
			String sender = message.getOriginatingAddress();
			if("#*location*#".equals(content)){
				GPSInfoProvider provider = GPSInfoProvider.getInstance(context);
				String location = provider.getLocation();
				Log.i(TAG, "=="+location);
				if("".equals(location)){
					
				}else{
					Log.i(TAG, "location不为空");
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.setClass(context, QueryAddressActivity.class);
					intent.putExtra("longitude", "106.720397");
					intent.putExtra("latitude", "26.597239");
					intent.putExtra("sender", sender);
					context.startActivity(intent);
				}
				//终止广播
				abortBroadcast();
				//http://www.cnblogs.com/ok-lanyan/archive/2012/07/13/2590288.html 百度api利用经纬度反向解析地址
				//http://developer.baidu.com/map/uri.htm
				//http://developer.baidu.com/map/
				//http://developer.baidu.com/
				//http://blog.csdn.net/longaohun/article/details/8219204 根据地址获取经纬度
				//http://blog.sina.com.cn/s/blog_876d072301014bm3.html
				//http://wenku.baidu.com/view/2c42bd11cc7931b765ce153e.html
			}else if("#*locknow*#".equals(content)){
				DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
				manager.resetPassword("123", 0);
				manager.lockNow();
				abortBroadcast();
			}else if("#*wipedata*#".equals(content)){
				DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
				manager.wipeData(0);
				abortBroadcast();
			}else if("#*alarm*#".equals(content)){
				MediaPlayer player = MediaPlayer.create(context, R.raw.ylzs);
				player.setVolume(1.0f, 1.0f);
				player.start();
				abortBroadcast();
			}
			if(dao.find(sender)){
				//黑名单中的短信
				abortBroadcast();
				//todo:把短信内容放到数据库里
			}
		}
	}
}
